package com.woniuxy.shop.web;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class WebListenerDemo implements HttpSessionListener {
	@Override
	public void sessionCreated(HttpSessionEvent se) {
		ServletContext context =  se.getSession().getServletContext();
		if(context.getAttribute("num")==null) {
			//如果为null,则表明是初次创建，赋值为1
			context.setAttribute("num", 1);
		}else{
			//不为null,则在得到的值的基础上+1
			context.setAttribute("num", (int)context.getAttribute("num")+1);
		}
	}
	
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		ServletContext context =  se.getSession().getServletContext();
		if(context.getAttribute("num")==null) {
			context.setAttribute("num", 0);
		}else {
			int num = (int) context.getAttribute("num");
			if(num>0) {
				context.setAttribute("num", num-1);
			}
		}
		
		
	}
	
	
}
